/**
 * Created by liyuanquan on 2016/12/22.
**/

var ObjHistoryUrl = serverPath + 'versionHistory/queryVersionHistoryByTypeIdAndObjectId';
var ObjVersionChangeUrl = serverPath + 'versionHistory/queryVersionHistoryById';
var ObjVersionChangeStatusUrl = serverPath + 'versionProcess/queryOptionalState';
var ObjVersionAddVersionHistoryUrl = serverPath + 'versionHistory/addVersionHistory';

var VersionDetailBox = 0;
var VersionHistoryBox = 0;
var VersionChangeDivIndex = 0;
var vIdTest = 102;

// 页面加载时执行
$(function() {
	'use strict';
    // 点击button1的事件
    $('#btn1').click(function() {
        var url = "VersionDetailCheck.html";
        $('#box1').load(url,function() {
            $("#VersionDetailTable").bootstrapTable();
            loadVersionDetailData(1);
        });
        VersionDetailBox = ibcpLayer.ShowDiv($("#box1"), '版本详细信息', '400px', '380px');
    });

    // 点击button2的事件
    $('#btn2').click(function() {
        var url = "ObjectVersionHistory.html";
        $('#box2').load(url,function() {
            $("#VersionHistoryTable").bootstrapTable();
            loadDataForObjHistory(1, 1);
        });
        VersionHistoryBox = ibcpLayer.ShowDiv($("#box2"), '版本历史信息', '900px', '380px');
    });

    $('#btn3').click(function() {
        var url = "ObjectStateChange.html";
        $('#VersionChangeIndex').load(url,function() {
            LoadVCInfo(vIdTest);
        });
        VersionChangeDivIndex = ibcpLayer.ShowDiv($("#VersionChangeIndex"), '版本变更', '420px', '480px');
    });
});

/*************************************************** 版本详细 ***************************************************/


// 弹出层1获取后台数据
function loadVersionDetailData(id){
    'use strict';
    var datas = {'id': id};

    ibcpAjax.Select(ObjVersionChangeUrl, datas, true, function(result){
        var data = result.data;
        $('#Object_Detail_Version').val(data.version);
        $('#Object_Detail_Version').attr('readonly', 'readonly');
        $('#Object_Detail_State').val(data.toName);
        $('#Object_Detail_State').attr('readonly', 'readonly');
        $('#Object_Detail_StartTime').val(data.validStart);
        $('#Object_Detail_StartTime').attr('readonly', 'readonly');
        $('#Object_Detail_EndTime').val(data.validEnd);
        $('#Object_Detail_EndTime').attr('readonly', 'readonly');
    });
}


function VSDetailClose(){
    layer.close(VersionDetailBox);
}

/*************************************************** 版本历史 ***************************************************/


// 弹出层2获取后台数据
function loadDataForObjHistory(objId, typeId){
    'use strict';
    var datas = {'objId': objId, 'typeId': typeId};

    ibcpAjax.Select(ObjHistoryUrl, datas, true, function(result){
        var data = result.data;
        $('#VersionHistoryTable').bootstrapTable('load', data);
    });
}

function VSHistoryClose(){
    layer.close(VersionHistoryBox);
}

/*************************************************** 版本变更 ***************************************************/

//当前版本信息
var objNowVS = '';

//加载当前版本信息
function LoadVCInfo(vId){
    var datas = {'id': vId};
    ibcpAjax.Select(ObjVersionChangeUrl, datas, true, function(result) {
        objNowVS = result.data;
        $('#CurrentVersion').val(objNowVS.version);
        $('#CurrentStatus').val(objNowVS.toName);
        LoadAllowChangeStatusInfo(objNowVS.toStatusId, objNowVS.typeId);
    });
}

//加载可变更状态
function LoadAllowChangeStatusInfo(fromId, typeId){
    var datas = {'fromId': fromId,'typeId': typeId};
    $("#targetStatus").attr('readonly', true);
    $("#ValidDateStart").attr('readonly',true);
    $("#ValidDateEnd").attr('readonly',true);
    $("#ChangeReason").attr('readonly',true);
    $("#btnVCSave").attr('disabled',true);

    ibcpAjax.Select(ObjVersionChangeStatusUrl, datas, true, function(result){
        var data = result.data
        if(data.length > 0){
            for(var i=0;i<data.length;i++) {
                var toOptions = "<option value='" + JSON.stringify(data[i]) + "'>" + data[i].toName + "</option>"
                $("#targetStatus").append(toOptions);
                $("#targetStatus").val('');
            }
            //只有一个数据的时候默认选择
            if(data.length == 1){
                $('#targetStatus').options[0].select = true;
            }
            $("#targetStatus").attr('readonly', false);
            $("#ValidDateStart").attr('readonly',false);
            $("#ValidDateEnd").attr('readonly',false);
            $("#ChangeReason").attr('readonly',false);
            $("#btnVCSave").attr('disabled',false);
        }
        else {
            ibcpLayer.ShowMsgCB('没有可变更的状态',function(){VSCCancel();});
        }
    });
}

//版本变更确认按钮事件
function VSCSave(){

    if($("#targetStatus").val() == null){
        ibcpLayer.ShowTips('请选择状态',$("#targetStatus"));
        return;
    }

    var seletToInfo = JSON.parse($("#targetStatus").val()); //选择的目标状态信息 是字符串 需要转Json
    var signNeedId = seletToInfo.aid;   //签名需要的ID
    var toId = seletToInfo.toId;         //目标状态ID
    var vStart = $("#ValidDateStart").val() + ' 00:00:00';
    var vEnd = $("#ValidDateEnd").val() + ' 23:59:59';
    var reason = $("#ChangeReason").val();

    //签名  根据目标状态获取签名id 调用签名 返回签名id
    var sid = 99;    //签名成功后返回的id


    var datas = {'verFlowId': objNowVS.verFlowId,
        'version': objNowVS.version,
        'fromStatusId': objNowVS.toStatusId,
        'toStatusId': toId,
        'validStart': vStart,
        'validEnd': vEnd,
        'sid': sid,
        'reason': reason,
        'objId': objNowVS.objId,
        'typeId': objNowVS.typeId
    };
    //保存
    ibcpAjax.Update(ObjVersionAddVersionHistoryUrl,datas,false,function(result){
        layer.close(VersionChangeDivIndex);
        ibcpLayer.ShowOK("操作成功！");
    });
}

//版本变更取消按钮事件
function VSCCancel(){
    layer.close(VersionChangeDivIndex);
}